<?php 

namespace Admin\Controller;
use Think\Controller;

/**
 * 用户管理控制器
 * @author 艾逗笔<765532665@qq.com>
 */
class OrderController extends BaseController {

    /**
     * 添加用户
     * @author 艾逗笔<765532665@qq.com>
     */
    public function add() {
    	echo "<pre>";  
             print_r($_SESSION);  
         echo "</pre>";  
         $this->ajaxReturn('0','写入数据成功','0');
        // // if (IS_POST) {
        //     //自动验证用户表(member)的字段值
        //     $_validate = array(
        //         array('telephone', 'require', '手机号不能为空', 1, 'regex', 3),
        //         array('telephone', '11', '请输入正确的手机号', 2, 'length', 1),
        //         array('nickname', 'require', '未能正确获取昵称', 1, 'regex', 1),
        //     );
        //     //连接数据库
        //     $member = M('member');
        //     $order = M('order');
        //     $goods = M('goods');
        //     $user_good_order = M('user_good_order');
        //     //验证member表
        //     $member->setProperty('_validate', $_validate);
            
        //     $mobile=$_POST['mobile'];
        //     $code=$_POST['code'];
        //     $mobile_notice=$_POST['mobile_notice'];
        //     //判断用户是否是会员
        //     $ishave=M()->table('member')->where('telephone= "'.$mobile.'"')->count();
        //     if($ishave != 0){//会员非首次购买

        //         $this->ajaxReturn('1','首次免费机会已使用','1');
        //         //以下直接插订单表不扣除费用

        //     }else{

        //         //会员首次购买插用户信息表
        //         if($_SESSION['code']==$code && $_SESSION['mobile']==$mobile){
        //             $data['city']='大连';
        //             $data['nickname']='彩迷123';
        //             $data['telephone']=$mobile;
        //             $data['createtime']=date('Y-m-d H:i:s',time());
        //             $results_member=$member->data($data)->add();
        //             if($results_member){
        //                 $this->ajaxReturn('0','写入数据成功','0');
        //             }else{
        //                 $this->ajaxReturn('2','写入数据失败','2');
        //             }
        //             // 首次用户订单插订单表
        //             $data['good_id']=$_POST['good_id'];
        //             $data['good_name']=$_POST['good_name'];
        //             //16位订单号
        //             $data['number']=substr(time().mt_rand(), 0,16);
        //             $results_order=$order->data($data)->add();
                    
        //             if($results_order){
        //                 $this->ajaxReturn('0','写入数据成功','0');
        //             }else{
        //                 $this->ajaxReturn('2','写入数据失败','2');
        //             }
                
        //         }else{
        //             $this->ajaxReturn('3','验证码错误','3');
        //         }
        //     }
        // } 
          
            
    }


    public function sendmassage(){
            $mobile = $_POST['mobile'];
            $code = $this->code();
            $target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
            $post_data = "account=cf_fbh&password=9393e424c57b23295513d55beb31822d&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$code."。请不要把验证码泄露给其他人。");
            $gets =  $this->xml_to_array($this->Post($post_data, $target));
            if($gets['SubmitResult']['code']==2){
                $_SESSION['mobile'] = $mobile;
                $_SESSION['code'] = $code;
            }
        $this->ajaxReturn('1','sucess','');
    }

    function code($length = 6 , $numeric = 0) {
        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
        if($numeric) {
            $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
            $hash = '';
            $chars = '01234567899876543210';
            $max = strlen($chars) - 1;
            for($i = 0; $i < $length; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
            }
        }
        return $hash;
    }
    function Post($curlPost,$url){
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_NOBODY, true);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
            $return_str = curl_exec($curl);
            curl_close($curl);
            return $return_str;
    }
 
	//将 xml数据转换为数组格式。
    function xml_to_array($xml){
        $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
        if(preg_match_all($reg, $xml, $matches)){
            $count = count($matches[0]);
            for($i = 0; $i < $count; $i++){
            $subxml= $matches[2][$i];
            $key = $matches[1][$i];
                if(preg_match( $reg, $subxml )){
                    $arr[$key] = $this->xml_to_array( $subxml );
                }else{
                    $arr[$key] = $subxml;
                }
            }
        }
        return $arr;
    }
    /**
     * 订单列表
     * 
     */
    public function lists() {
        $this->addCrumb('系统管理', U('Index/index'), '')
             ->addCrumb('订单管理', U('Goods/lists'), '')
             ->addCrumb('订单列表', '', 'active')
             ->addNav('订单列表', '', 'active')
             ->setModel('order')
             ->setListOrder('id desc')
             ->setListSearch(array(
                'number' => '订单号',
                'buytime' => '下单时间'
             ))
             ->addListItem('id', '订单ID')
             ->addListItem('goods_name', '商品名称')
             ->addListItem('member_id', '用户名称', 'callback', array('callback_name'=>'order_member'))
             ->addListItem('buytime', '下单时间', 'function', array('function_name'=>'date','params'=>'Y-m-d H:i:s,###'))
             ->addListItem('status', '状态')
             // ->addListItem('status', '用户角色', 'callback', array('callback_name'=>'parse_roles'))
             // ->addListItem('register_time', '注册时间', 'function', array('function_name'=>'date','params'=>'Y-m-d H:i:s,###'))
             ->addListItem('id', '操作', 'custom', array('options'=>array('edit'=>array('编辑',U('edit',array('id'=>'{id}')),'btn btn-primary btn-sm icon-edit',''),'delete'=>array('删除',U('delete',array('id'=>'{id}')),'btn btn-danger btn-sm icon-delete', ''))))
             ->setListPer(30)
             ->common_lists();
    }

    public function get_goods_name() {
        $results = M('goods')->select();
        foreach ($results as $k => $v) {
            $goods[$v['id']] = $v['name'];
        }
        return $goods;
    }

    public function get_user_name() {
        $results = M('user')->select();
        foreach ($results as $k => $v) {
            $users[$v['id']] = $v['username'];
        }
        return $users;
    }
	
    public function order_member($member_id) {
        $results = M('member')->field('nickname')->where(array('id'=>$member_id))->find();
        return $results['nickname'];
    }


    /**
     * 格式化显示用户所属角色
     * @author 艾逗笔<765532665@qq.com>
     */
    public function order_goods($good_id) {
        $order_goods = M('user_good_order')->field('good_id')->where(array('good_id'=>$good_id))->find();
        foreach ($order_goods as $k => $v) {
            $good = M('goods')->where(array('id'=>$v['good_id']))->getField('name');
            $goods[] = $good;
        }
        return implode(',', $goods);
    }



    public function order_user($user_id) {
        $order_users = M('user_good_order')->field('user_id')->where(array('user_id'=>$user_id))->find();
        foreach ($order_users as $k => $v) {
            $user = M('user')->where(array('id'=>$v['user_id']))->getField('username');
            $users[] = $user;
        }
        return implode(',', $users);
    }   

}

?>